using System;
using System.Collections.Generic;
using System.Text;

using System.Management.Automation;

namespace Microsoft.PowerShell.CmdletManagement
{
    [Cmdlet(VerbsCommon.Add, "PsSpecUserPermission", SupportsShouldProcess = true)]
    public class AddPsSpecUserPermissionCommand : Cmdlet
    {
        [ValidateLength(1, 50)]
        [Parameter(Mandatory = true, Position = 0, ValueFromPipelineByPropertyName = true)]
        public string ProjectName
        {
            get { return projectName; }
            set { projectName = value; }
        }
        private string projectName = null;

        [Parameter(Mandatory = true, Position = 1, ValueFromPipelineByPropertyName = true)]
        [ValidateLength(1, 50)]
        public String User
        {
            get
            {
                return _user;
            }
            set
            {
                _user = value;
            }
        }
        private string _user = null;

        [Parameter(Mandatory = true, Position = 2, ValueFromPipelineByPropertyName = true)]
        public PsSpecProjectRole Role
        {
            get
            {
                return _role;
            }
            set
            {
                _role = value;
            }
        }
        private PsSpecProjectRole _role = PsSpecProjectRole.Unspecified;

        [Parameter(Mandatory = false)]
        public String ServiceUri
        {
            get
            {
                return _serviceUri;
            }
            set
            {
                _serviceUri = value;
            }
        }
        private string _serviceUri = PsSpecCmdletUtils.Constants.ServiceUri;


        protected override void ProcessRecord()
        {
            if (ShouldProcess(
                String.Format("{0}", ServiceUri),
                String.Format(resources.AddProjectUserShouldProcess, User, ProjectName, Role)))
            {
                PsSpecCmdletService.CmdletService service =
                    new PsSpecCmdletService.CmdletService();
                service.Url = ServiceUri;
                service.UseDefaultCredentials = true;

                try
                {
                    service.NewPsSpecUserPermission(ProjectName, User, Role.ToString());
                }
                catch (System.Web.Services.Protocols.SoapException e)
                {
                    WriteError(new ErrorRecord(e, "CMDLET_SERVICE_ERROR",
                                                ErrorCategory.InvalidOperation,
                                                String.Format(resources.AddProjectUserShouldProcess, User, ProjectName, Role)));
                }
            }
        }
    }
}
